package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Board;
import com.example.forum.services.IBoradService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags ="板块接口")
@Slf4j
@RequestMapping(("board"))
public class BoardController {

    @Resource
    private IBoradService boardService;

    @Value("${bit-forum.index.board-num:9}")
    private Integer indexBoardNum ; // 如果在首页没有拿到参数,默认值是9
    /**
     * 查询首页板块列表
     * @return
     */
    @GetMapping("/topList")
    @ApiOperation("获取首页列表")
    public AppResult<List<Board>> topList() {
        log.info("配置首页板块个数为" + indexBoardNum);
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        if (boards == null) {
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }

    @ApiOperation("获取版块信息")
    @GetMapping("getById")
    public AppResult<Board> getById (
            @ApiParam("版块id")@RequestParam("id")@NonNull Long id) {
        Board board = boardService.selectById(id);
        if (board == null || board.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

        return AppResult.success(board);
    }

}
